<?php
/**
 * 卡片
 * User: Acer
 * Date: 2022/7/9
 * Time: 0:50
 */
namespace app\card\controller;

use app\core\MyController;
use app\card\service\CardService;
class Card extends MyController{
    protected  $noNeedAction = []; // 不需要校验控制器，通道
    // 操作前置
    protected $beforeActionList = ['my_initialize'];
    // 操作前置方法
    public function my_initialize(){
//        $this->is_admin();  // 权限检查

    }
    // 新增卡片
    public function add_card(){
        $data = input();
        if(empty($data['ct_id']) || empty($data['name'])|| empty($data['prefix'])|| empty($data['suffix'])|| empty($data['middle']) ) json_err( -201);
        $res = (new CardService())->s_add_card($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }

    //卡片列表
    public function card_list(){
        $data = input();
        // 页数
        if(!isset($data['page']))  json_err( -201);
        // 当前页数
        if(!isset($data['page_number'])) $data['page_number'] = 10;

        // 查询
        $like =[];
        // 卡片名称
        if(isset($data['name']) && $data['name'] ) $like['name'] = $data['name'];
        // 分类
        if(isset($data['category']) && $data['category']) $like['category'] = $data['category'];
        $res = (new CardService())->s_card_list($data['page'],$data['page_number'] > 0 ? $data['page_number'] : 1,$like);
        if($res) _action_result(200,$res,'操作成功！');
        json_err( -205);
    }

    // 修改卡片信息
    public function modify_card(){
        $data = input();
        if(empty($data['ct_id'])  || empty($data['cid']) || empty($data['name']) || empty($data['prefix'])|| empty($data['suffix'])|| empty($data['middle'])) json_err( -201);
        $res = (new CardService())->s_modify_card($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }
    // 修改卡片状态
    public function state_card(){
        $data = input();
        if( empty($data['cid'])) json_err( -201);
        $res = (new CardService())->s_state_card($data);
        if($res) _action_result(200,[],'操作成功！');
        json_err( -205);
    }
    // 根据激活码，获取卡片信息
    public function getCardInfo(){
        $data = input();
	    if(empty($data['active_code'])&&empty($data['pid'])) json_err( -201);
	    $data['uid'] = $this->userId;
        $res = (new CardService())->s_get_card_info($data);
        if($res) _action_result(200,$res,'操作成功！');
        json_err( -205);
    }
    // 根据卡号获取卡片可激活时间范围
    public function getCardActivePeriod(){
        $data = input();
        if(empty($data['code'])) json_err( -201);
        $res = (new CardService())->s_get_card_active_period($data);
        if($res) _action_result(200,$res,'操作成功！');
        json_err( -205);
    }

}
